﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OpenQuarters.WebQuarters.Utils;
using OpenQuarters.WebQuarters.Core;
using System.IO;

namespace OpenQuarters.WebQuarters.Modules.Core
{
    public class ImageModule : Module
    {
        public static Dictionary<string, string> Templates
        {
            get
            {
                var d = new Dictionary<string, string>();
                d.Add("", "Default");
                foreach (var dir in new DirectoryInfo(Oracle.Context.Server.MapPath("~/Templates/")).GetDirectories())
                {
                    if (dir.Name.Contains(typeof(ImageModule).FullName))
                    {
                        foreach (var template in dir.GetFiles("*.ascx", SearchOption.TopDirectoryOnly))
                        {
                            d.Add(template.Name.Replace(".ascx", ""), template.Name.Replace(".ascx", ""));
                        }
                    }
                }
                return d;
            }
        }

        private string link = "";

        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.TextBox)]
        public string Link
        {
            get { return link; }
            set { link = value; }
        }

        private string imagePath = "/Areas/CMS/Content/Images/image_imagenotfound.gif";

        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.Image)]
        public string Image_Path
        {
            get { return imagePath; }
            set { imagePath = value; }
        }

        private string alt = "";

        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.TextBox)]
        public string Title_Alt_Text
        {
            get { return alt; }
            set { alt = value; }
        }

        private string template = "";

        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.DropDown, OptionSource = "OpenQuarters.WebQuarters.Modules.Core.ImageModule.Templates")]
        public string Template
        {
            get { return template; }
            set { template = value; }
        }


        public override void Initialise()
        {
            this.ModuleName = "Image Module";
            this.ModuleDescription = "Places a single image on the page.";
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            if (OpenQuarters.WebQuarters.Core.User.Current.HasPermission(OpenQuarters.WebQuarters.Core.Permissions.CMSPage.Edit.SetValue(OpenQuarters.WebQuarters.Core.CMSPage.Current.PageId.ToString())))
            {
                CSS.RegisterStyleSheet("Modules.Core.ImageModule.Admin.css", "OpenQuarters.WebQuarters.Modules.Core", "Resources.Image.Admin.css", CSSType.AdminStyleSheet);
                WebQuarters.Core.JavaScript.RegisterScript("Modules.Image.AdminStatupScript.js", "OpenQuarters.WebQuarters.Modules.Core", "Resources.Image.AdminStartupScript.js", JavaScriptType.AdminStartupScript);
            }

            if (this.Template.Length == 0)
            {
                if (link.Length != 0)
                {

                    if (OpenQuarters.WebQuarters.Core.User.Current.HasPermission(OpenQuarters.WebQuarters.Core.Permissions.CMSPage.Edit.SetValue(OpenQuarters.WebQuarters.Core.CMSPage.Current.PageId.ToString())))
                    {
                        base.Controls.Add(new LiteralControl(string.Format("<img src=\"{0}\" alt=\"{1}\" title=\"{1}\" />", Image_Path, alt)));

                    }
                    else
                    {
                        HyperLink image = new HyperLink();
                        image.NavigateUrl = link;
                        image.ImageUrl = Image_Path;
                        image.ToolTip = alt;
                        base.Controls.Add(image);
                    }
                }
                else
                {
                    base.Controls.Add(new LiteralControl(string.Format("<img src=\"{0}\" alt=\"{1}\" title=\"{1}\" />", Image_Path, alt)));
                }
            }
            else
            {
                var viewPath = "~/Templates/" + this.GetType().FullName + "/" + this.Template + ".ascx";
                //this.Controls.AddLiteral(GeneralUtils.RenderViewToString(
                //        Oracle.CurrentControllerContext,
                //        viewPath,
                //        null,
                //        new ViewDataDictionary(this),
                //        new TempDataDictionary()));
                this.Controls.AddLiteral(GeneralUtils.RenderPartialToString(viewPath, this));
            }
        }
    }

}
